export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

export const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

export const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1 //8
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  
  return [year, month, day].map(formatNumber).join('-');
}


export const formatDateChain = (ns) => {
  var allStr = getLocalTime(ns);
  var year = allStr.substr(0, 4);
  var month = allStr.substr(5, 2);
  var day = allStr.substr(8, 2);
  return year + '年' + month + '月' + day + '日';
}

const getLocalTime = (ns) => {
  //needTime是整数，否则要parseInt转换  
  var time = new Date(parseInt(ns) * 1000); //根据情况*1000
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

const add0 =(m) => {
  return m < 10 ? '0' + m : m
}
